import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index"

Vue.use(VueRouter);

import Home from "@/views/buyer/Home.vue";

//import Shopcart from "@/views/Shopcart.vue";
const Shopcart = () => import(/* webpackChunkName: "shopcart" */"@/views/buyer/Shopcart.vue");

import User from "@/views/User.vue";
import Register from "@/views/user/Register.vue";
import GoodsList from "@/views/goods/GoodsList.vue";
import GoodsInfo from "@/views/goods/GoodsInfo.vue";
import AddressList from "@/views/user/AddressList.vue";
import AddressEdit from "@/views/user/AddressEdit.vue";
import OrderCreate from "@/views/order/OrderCreate.vue";
import OrderList from "@/views/order/OrderList.vue";
import OrderShow from "@/views/order/OrderShow.vue";
import PhotoList from "@/views/photo/PhotoList.vue";
import PhotoInfo from "@/views/photo/PhotoInfo.vue";


import {getToken} from "@/utils/auth.js";

import Welcome from "@/views/Welcome";
import Wallet from "../views/Wallet.vue"
/*buyer*/
import BuyerInfo from "../views/buyer/BuyerInfo.vue"

/*seller*/
import SellerHome from "@/views/seller/Home.vue"
import SellerGoods from "@/views/seller/Goods.vue"
import SellerOrders from "../views/seller/SellerOrders";
import SellerAddresses from "../views/seller/SellerAddresses";
import BuyerEvaluate from "@/views/buyer/BuyerEvaluate";
import OrderPay from "../views/order/OrderPay";
import GoodsDetail from "@/views/goods/GoodsDetail";
import DeliveryNotice from "@/views/seller/DeliveryNotice";

const routes = [

    {path: "/welcome", component: Welcome, name: "welcome", meta: {title: "欢迎页", keepAlive: true}},

    {path: "/home", component: Home, name: "home", meta: {title: "首页", keepAlive: true}},
    {
        path: "/shopcart",
        component: Shopcart,
        name: "shopcart",
        meta: {title: "购物车", requireAuth: true},
    },
    {path: "/user", component: User, name: "user", meta: {title: "我的"}},
    {
        path: "/user/register",
        component: Register,
        name: "register",
        meta: {title: "注册"},
    },
    {path: "/buyer/info", component: BuyerInfo, name: "buyerInfo", meta: {title: "买家信息修改"}},
    {path: "/seller/notice", component: DeliveryNotice, name: "deliveryNotice", meta: {title: "发货提醒"}},

    {path: "/buyer/evaluate", component: BuyerEvaluate, name: "buyerEvaluate", meta: {title: "商品评价"}},
    {path: "/order/pay", component: OrderPay, name: "orderPay", meta: {title: "支付"}},

    {path: "/user/wallet", component: Wallet, name: "wallet", meta: {title: "钱包"}},

    {path: "/seller/home", component: SellerHome, name: "sellerHome", meta: {title: "发布闲置"}},
    {path: "/seller/goods", component: SellerGoods, name: "sellerGoods", meta: {title: "在售商品"}},
    {path: "/seller/user", component: User, name: "seller", meta: {title: "卖家中心"}},
    {path: "/seller/orders", component: SellerOrders, name: "sellerOrders", meta: {title: "卖家订单"}},
    {path: "/seller/addresses", component: SellerAddresses, name: "sellerAddresses", meta: {title: "发货地址"}},

    {
        path: "/goodslist/:category_id",
        component: GoodsList,
        props: true,
        name: "goods_list",
        meta: {title: "商品列表"},
    },
    {
        path: "/goodsinfo/:id",
        component: GoodsInfo,
        props: true,
        name: "goods_info",
        meta: {title: "商品信息"},
    },
    {
        path: "/goodsDetail/:id",
        component: GoodsDetail,
        props: true,
        name: "goodsDetail",
        meta: {title: "商品详情"},
    },
    {
        path: "/user/address/list",
        component: AddressList,
        name: "address_list",
        meta: {title: "收货地址", requireAuth: true},
    },
    {
        path: "/user/address/select",
        component: AddressList,
        name: "address_select",
        meta: {title: "选择收货地址", requireAuth: true},
    },
    {
        path: "/user/address/add",
        component: AddressEdit,
        name: "address_add",
        meta: {title: "新增收货地址", requireAuth: true},
    },
    {
        path: "/user/address/edit/:id",
        component: AddressEdit,
        props: true,
        name: "address_edit",
        meta: {title: "编辑收货地址", requireAuth: true},
    },
    {
        path: "/order/create",
        component: OrderCreate,
        name: "order_create",
        meta: {title: "下订单", requireAuth: true},
    },

    {
        path: "/order/list",
        component: OrderList,
        name: "order_list",
        meta: {title: "我的订单", requireAuth: true},
    },
    {
        path: "/order/show/:id",
        component: OrderShow,
        props: true,
        name: "order_show",
        meta: {title: "查看订单", requireAuth: true},
    },
    {
        path: "/photo/list",
        component: PhotoList,
        name: "photo_list",
        meta: {title: "图片分享列表"},
    },
    {
        path: "/photo/show/:id",
        component: PhotoInfo,
        props: true,
        name: "photo_show",
        meta: {title: "查看图片"},
    },
];
const router = new VueRouter({
    // 解决路由跳转页面没有置顶
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {
                x: 0,
                y: 0,
            };
        }
    },
    routes,
});
const anon = ['login', "welcome", "register"]

router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    let isLogin = false
    let name = undefined
    let index = 0
    switch (to.name) {
        case "sellerHome":
        case "home":
            index = 0;
            break
        case "sellerGoods":
        case "shopcart":
            index = 1;
            break
        case "user":
        case "deliveryNotice":
            index = 2;
            break
        case "seller":
            index = 3;
            break
        default:
            break;
    }
    to.meta["index"]=index
    console.log("change", index)
    if (anon.indexOf(to.name) == -1) {  // 路由需要验证的
        if (getToken()) {
            isLogin = true
        } else if (name !== 'welcome') {
            name = 'welcome'
        }
    }
    store.commit("user/setIsLogin", isLogin)
    if (name === undefined) {
        next()
    } else {
        next({name: name});
    }
});


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

export default router;
